
<!doctype html>
<html lang="zh" class="no-js">
  <head>
    
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1">
      
      
      
      
      <link rel="icon" href="../../assets/images/favicon.png">
      <meta name="generator" content="mkdocs-1.2.3, mkdocs-material-7.3.6">
    
    
      
        <title>编写测试用例 - HttpRunner V3.x中文文档</title>
      
    
    
      <link rel="stylesheet" href="../../assets/stylesheets/main.a57b2b03.min.css">
      
        
        <link rel="stylesheet" href="../../assets/stylesheets/palette.3f5d1f46.min.css">
        
      
    
    
    
      
        
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,400i,700%7CRoboto+Mono&display=fallback">
        <style>:root{--md-text-font-family:"Roboto";--md-code-font-family:"Roboto Mono"}</style>
      
    
    
    
    
      


    
    
  </head>
  
  
    
    
    
    
    
    <body dir="ltr" data-md-color-scheme="" data-md-color-primary="none" data-md-color-accent="none">
  
    
    <script>function __prefix(e){return new URL("../..",location).pathname+"."+e}function __get(e,t=localStorage){return JSON.parse(t.getItem(__prefix(e)))}</script>
    
    <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
    <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
    <label class="md-overlay" for="__drawer"></label>
    <div data-md-component="skip">
      
        
        <a href="#_1" class="md-skip">
          跳转至
        </a>
      
    </div>
    <div data-md-component="announce">
      
    </div>
    
      

<header class="md-header" data-md-component="header">
  <nav class="md-header__inner md-grid" aria-label="Header">
    <a href="../.." title="HttpRunner V3.x中文文档" class="md-header__button md-logo" aria-label="HttpRunner V3.x中文文档" data-md-component="logo">
      
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54z"/></svg>

    </a>
    <label class="md-header__button md-icon" for="__drawer">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2z"/></svg>
    </label>
    <div class="md-header__title" data-md-component="header-title">
      <div class="md-header__ellipsis">
        <div class="md-header__topic">
          <span class="md-ellipsis">
            HttpRunner V3.x中文文档
          </span>
        </div>
        <div class="md-header__topic" data-md-component="header-topic">
          <span class="md-ellipsis">
            
              编写测试用例
            
          </span>
        </div>
      </div>
    </div>
    
    
    
      <label class="md-header__button md-icon" for="__search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5z"/></svg>
      </label>
      
<div class="md-search" data-md-component="search" role="dialog">
  <label class="md-search__overlay" for="__search"></label>
  <div class="md-search__inner" role="search">
    <form class="md-search__form" name="search">
      <input type="text" class="md-search__input" name="query" aria-label="搜索" placeholder="搜索" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="search-query" required>
      <label class="md-search__icon md-icon" for="__search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5z"/></svg>
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12z"/></svg>
      </label>
      <nav class="md-search__options" aria-label="Search">
        
        <button type="reset" class="md-search__icon md-icon" aria-label="Clear" tabindex="-1">
          <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41z"/></svg>
        </button>
      </nav>
      
    </form>
    <div class="md-search__output">
      <div class="md-search__scrollwrap" data-md-scrollfix>
        <div class="md-search-result" data-md-component="search-result">
          <div class="md-search-result__meta">
            正在初始化搜索引擎
          </div>
          <ol class="md-search-result__list"></ol>
        </div>
      </div>
    </div>
  </div>
</div>
    
    
  </nav>
  
</header>
    
    <div class="md-container" data-md-component="container">
      
      
        
          
        
      
      <main class="md-main" data-md-component="main">
        <div class="md-main__inner md-grid">
          
            
              
              <div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    


<nav class="md-nav md-nav--primary" aria-label="Navigation" data-md-level="0">
  <label class="md-nav__title" for="__drawer">
    <a href="../.." title="HttpRunner V3.x中文文档" class="md-nav__button md-logo" aria-label="HttpRunner V3.x中文文档" data-md-component="logo">
      
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54z"/></svg>

    </a>
    HttpRunner V3.x中文文档
  </label>
  
  <ul class="md-nav__list" data-md-scrollfix>
    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../.." class="md-nav__link">
        名词解释
      </a>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../../10-HttpRunner%E7%AE%80%E4%BB%8B/" class="md-nav__link">
        简介
      </a>
    </li>
  

    
      
      
      

  
  
  
    <li class="md-nav__item">
      <a href="../../20-HttpRunner%E5%AE%89%E8%A3%85/" class="md-nav__link">
        安装
      </a>
    </li>
  

    
      
      
      

  
  
    
  
  
    
    <li class="md-nav__item md-nav__item--active md-nav__item--nested">
      
      
        <input class="md-nav__toggle md-toggle" data-md-toggle="__nav_4" type="checkbox" id="__nav_4" checked>
      
      
      
      
        <label class="md-nav__link" for="__nav_4">
          用户指南
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" aria-label="用户指南" data-md-level="1">
        <label class="md-nav__title" for="__nav_4">
          <span class="md-nav__icon md-icon"></span>
          用户指南
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../31-%E6%A6%82%E8%BF%B0/" class="md-nav__link">
        HttpRunner概念
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../32-%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8B/" class="md-nav__link">
        快速上手
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../33-%E5%BD%95%E5%88%B6%E5%B9%B6%E7%94%9F%E6%88%90%E6%B5%8B%E8%AF%95%E6%8A%A5%E5%91%8A/" class="md-nav__link">
        录制并生成测试用例
      </a>
    </li>
  

            
          
            
              
  
  
    
  
  
    <li class="md-nav__item md-nav__item--active">
      
      <input class="md-nav__toggle md-toggle" data-md-toggle="toc" type="checkbox" id="__toc">
      
      
        
      
      
        <label class="md-nav__link md-nav__link--active" for="__toc">
          编写测试用例
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <a href="./" class="md-nav__link md-nav__link--active">
        编写测试用例
      </a>
      
        


<nav class="md-nav md-nav--secondary" aria-label="目录">
  
  
  
    
  
  
    <label class="md-nav__title" for="__toc">
      <span class="md-nav__icon md-icon"></span>
      目录
    </label>
    <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#_2" class="md-nav__link">
    记录并生成测试用例
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_3" class="md-nav__link">
    测试用例结构
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_4" class="md-nav__link">
    链式调用
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#config" class="md-nav__link">
    一、config
  </a>
  
    <nav class="md-nav" aria-label="一、config">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#name" class="md-nav__link">
    name（必填）
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#base_url" class="md-nav__link">
    base_url（可选）
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#variables" class="md-nav__link">
    variables（可选）
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#verify" class="md-nav__link">
    verify（可选）
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#export" class="md-nav__link">
    export（可选）
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
        <li class="md-nav__item">
  <a href="#teststeps" class="md-nav__link">
    二、teststeps
  </a>
  
    <nav class="md-nav" aria-label="二、teststeps">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#1-runrequestname" class="md-nav__link">
    1. RunRequest(name)
  </a>
  
    <nav class="md-nav" aria-label="1. RunRequest(name)">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#with_variables" class="md-nav__link">
    .with_variables
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#methodurl" class="md-nav__link">
    .method（url）
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#with_params" class="md-nav__link">
    .with_params
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#with_headers" class="md-nav__link">
    .with_headers
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#with_cookies" class="md-nav__link">
    .with_cookies
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#whth_data" class="md-nav__link">
    .whth_data
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#with_json" class="md-nav__link">
    .with_json
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#2-extract" class="md-nav__link">
    2. extract
  </a>
  
    <nav class="md-nav" aria-label="2. extract">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#with_jmespath" class="md-nav__link">
    .WITH_JMESPATH
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#3-validate" class="md-nav__link">
    3. validate
  </a>
  
    <nav class="md-nav" aria-label="3. validate">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#assert_xxx" class="md-nav__link">
    .ASSERT_XXX
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#4-runtestcasename" class="md-nav__link">
    4. RunTestCase（name）
  </a>
  
    <nav class="md-nav" aria-label="4. RunTestCase（name）">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#with_variables_1" class="md-nav__link">
    .with_variables
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#call" class="md-nav__link">
    .call
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#export_1" class="md-nav__link">
    .export
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
    </ul>
  
</nav>
      
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../35-%E8%BF%90%E8%A1%8C%E6%B5%8B%E8%AF%95%E7%94%A8%E4%BE%8B/" class="md-nav__link">
        运行测试用例
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../36-%E6%B5%8B%E8%AF%95%E6%8A%A5%E5%91%8A/" class="md-nav__link">
        测试报告
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../37-%E8%BF%90%E8%A1%8C%E8%B4%9F%E8%BD%BD%E6%B5%8B%E8%AF%95/" class="md-nav__link">
        运行负载测试
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../39-%E5%8F%91%E5%B8%83%E5%8E%86%E5%8F%B2/" class="md-nav__link">
        发布历史
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
      
      
      

  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
        <input class="md-nav__toggle md-toggle" data-md-toggle="__nav_5" type="checkbox" id="__nav_5" >
      
      
      
      
        <label class="md-nav__link" for="__nav_5">
          补充
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" aria-label="补充" data-md-level="1">
        <label class="md-nav__title" for="__nav_5">
          <span class="md-nav__icon md-icon"></span>
          补充
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../%E8%A1%A5%E5%85%85/10-%E9%A1%B9%E7%9B%AE%E6%96%87%E4%BB%B6%E7%BB%84%E7%BB%87/" class="md-nav__link">
        项目文件组织
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../%E8%A1%A5%E5%85%85/20-%E6%B5%8B%E8%AF%95%E7%94%A8%E4%BE%8B%E7%BB%84%E7%BB%87/" class="md-nav__link">
        测试用例组织
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../%E8%A1%A5%E5%85%85/30-Hook%E6%9C%BA%E5%88%B6/" class="md-nav__link">
        Hook机制
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../%E8%A1%A5%E5%85%85/40-%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F/" class="md-nav__link">
        环境变量
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../%E8%A1%A5%E5%85%85/50-%E6%B5%8B%E8%AF%95%E7%94%A8%E4%BE%8B%E5%88%86%E5%B1%82/" class="md-nav__link">
        测试用例分层
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../%E8%A1%A5%E5%85%85/60-%E5%8F%82%E6%95%B0%E5%8C%96%E6%95%B0%E6%8D%AE%E9%A9%B1%E5%8A%A8/" class="md-nav__link">
        参数化数据驱动
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../%E8%A1%A5%E5%85%85/70-%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9C%BA%E6%99%AF/" class="md-nav__link">
        文件上传场景
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
  </ul>
</nav>
                  </div>
                </div>
              </div>
            
            
              
              <div class="md-sidebar md-sidebar--secondary" data-md-component="sidebar" data-md-type="toc" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    


<nav class="md-nav md-nav--secondary" aria-label="目录">
  
  
  
    
  
  
    <label class="md-nav__title" for="__toc">
      <span class="md-nav__icon md-icon"></span>
      目录
    </label>
    <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#_2" class="md-nav__link">
    记录并生成测试用例
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_3" class="md-nav__link">
    测试用例结构
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#_4" class="md-nav__link">
    链式调用
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#config" class="md-nav__link">
    一、config
  </a>
  
    <nav class="md-nav" aria-label="一、config">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#name" class="md-nav__link">
    name（必填）
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#base_url" class="md-nav__link">
    base_url（可选）
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#variables" class="md-nav__link">
    variables（可选）
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#verify" class="md-nav__link">
    verify（可选）
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#export" class="md-nav__link">
    export（可选）
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
        <li class="md-nav__item">
  <a href="#teststeps" class="md-nav__link">
    二、teststeps
  </a>
  
    <nav class="md-nav" aria-label="二、teststeps">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#1-runrequestname" class="md-nav__link">
    1. RunRequest(name)
  </a>
  
    <nav class="md-nav" aria-label="1. RunRequest(name)">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#with_variables" class="md-nav__link">
    .with_variables
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#methodurl" class="md-nav__link">
    .method（url）
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#with_params" class="md-nav__link">
    .with_params
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#with_headers" class="md-nav__link">
    .with_headers
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#with_cookies" class="md-nav__link">
    .with_cookies
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#whth_data" class="md-nav__link">
    .whth_data
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#with_json" class="md-nav__link">
    .with_json
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#2-extract" class="md-nav__link">
    2. extract
  </a>
  
    <nav class="md-nav" aria-label="2. extract">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#with_jmespath" class="md-nav__link">
    .WITH_JMESPATH
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#3-validate" class="md-nav__link">
    3. validate
  </a>
  
    <nav class="md-nav" aria-label="3. validate">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#assert_xxx" class="md-nav__link">
    .ASSERT_XXX
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#4-runtestcasename" class="md-nav__link">
    4. RunTestCase（name）
  </a>
  
    <nav class="md-nav" aria-label="4. RunTestCase（name）">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#with_variables_1" class="md-nav__link">
    .with_variables
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#call" class="md-nav__link">
    .call
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#export_1" class="md-nav__link">
    .export
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
    </ul>
  
</nav>
                  </div>
                </div>
              </div>
            
          
          <div class="md-content" data-md-component="content">
            <article class="md-content__inner md-typeset">
              
                
                
                <h1 id="_1">编写测试用例</h1>
<p>httprunnerv3.x支持三种测试用例格式：pytest、YAML和JSON。非常推荐使用pytest格式而不是以前的YAML/JSON格式编写和维护测试用例。</p>
<p>格式关系如下图所示：</p>
<p><img alt="img" src="../img/httprunner-formats.png" /></p>
<h2 id="_2">记录并生成测试用例</h2>
<p>如果SUT（system under test）准备就绪，最有效的方法是首先捕获HTTP流量，然后使用HAR文件生成测试用例。有关更多详细信息，请参阅记录和生成测试用例。</p>
<p>根据生成的pytest测试用例，您可以根据需要进行一些调整，因此您需要了解测试用例格式的细节。</p>
<h2 id="_3">测试用例结构</h2>
<p>每个testcase都是HttpRunner的一个子类，必须有两个类属性：config和teststeps。</p>
<ul>
<li>
<p>配置：配置测试用例级别的设置，包括基本url、验证、变量、导出。</p>
</li>
<li>
<p>teststeps：teststep的列表（list[Step]），每个步骤对应于一个API请求或另一个testcase引用调用。此外，还支持变量/提取/验证/挂钩机制来创建极其复杂的测试场景。</p>
</li>
</ul>
<pre><code class="language-python">from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase

class TestCaseRequestWithFunctions(HttpRunner):
    config = (
        Config(&quot;request methods testcase with functions&quot;)
        .variables(
            **{
                &quot;foo1&quot;: &quot;config_bar1&quot;,
                &quot;foo2&quot;: &quot;config_bar2&quot;,
                &quot;expect_foo1&quot;: &quot;config_bar1&quot;,
                &quot;expect_foo2&quot;: &quot;config_bar2&quot;,
            }
        )
        .base_url(&quot;https://postman-echo.com&quot;)
        .verify(False)
        .export(*[&quot;foo3&quot;])
    )

    teststeps = [
        Step(
            RunRequest(&quot;get with params&quot;)
            .with_variables(
                **{&quot;foo1&quot;: &quot;bar11&quot;, &quot;foo2&quot;: &quot;bar21&quot;, &quot;sum_v&quot;: &quot;${sum_two(1, 2)}&quot;}
            )
            .get(&quot;/get&quot;)
            .with_params(**{&quot;foo1&quot;: &quot;$foo1&quot;, &quot;foo2&quot;: &quot;$foo2&quot;, &quot;sum_v&quot;: &quot;$sum_v&quot;})
            .with_headers(**{&quot;User-Agent&quot;: &quot;HttpRunner/${get_httprunner_version()}&quot;})
            .extract()
            .with_jmespath(&quot;body.args.foo2&quot;, &quot;foo3&quot;)
            .validate()
            .assert_equal(&quot;status_code&quot;, 200)
            .assert_equal(&quot;body.args.foo1&quot;, &quot;bar11&quot;)
            .assert_equal(&quot;body.args.sum_v&quot;, &quot;3&quot;)
            .assert_equal(&quot;body.args.foo2&quot;, &quot;bar21&quot;)
        ),
        Step(
            RunRequest(&quot;post form data&quot;)
            .with_variables(**{&quot;foo2&quot;: &quot;bar23&quot;})
            .post(&quot;/post&quot;)
            .with_headers(
                **{
                    &quot;User-Agent&quot;: &quot;HttpRunner/${get_httprunner_version()}&quot;,
                    &quot;Content-Type&quot;: &quot;application/x-www-form-urlencoded&quot;,
                }
            )
            .with_data(&quot;foo1=$foo1&amp;foo2=$foo2&amp;foo3=$foo3&quot;)
            .validate()
            .assert_equal(&quot;status_code&quot;, 200)
            .assert_equal(&quot;body.form.foo1&quot;, &quot;$expect_foo1&quot;)
            .assert_equal(&quot;body.form.foo2&quot;, &quot;bar23&quot;)
            .assert_equal(&quot;body.form.foo3&quot;, &quot;bar21&quot;)
        ),
    ]


if __name__ == &quot;__main__&quot;:
    TestCaseRequestWithFunctions().test_start()
</code></pre>
<h2 id="_4">链式调用</h2>
<p>HttpRunnerV3.x最棒的特性之一是链式调用，使用链式调用，您不需要记住任何测试用例格式的详细信息，并且在IDE中编写测试用例时可以智能完成。</p>
<p><img alt="img" src="https://docs.httprunner.org/images/httprunner-chain-call-config.png" /></p>
<p><img alt="img" src="https://docs.httprunner.org/images/httprunner-chain-call-step-validate.png" /></p>
<h2 id="config">一、config</h2>
<p>每个测试用例都应该有一个配置部分，您可以在其中配置测试用例级别的设置。</p>
<h3 id="name">name（必填）</h3>
<p>指定testcase名称。这将显示在执行日志和测试报告中。</p>
<h3 id="base_url">base_url（可选）</h3>
<p>指定SUT的公共架构和主机部分，例如。https://postman-echo.com. 如果指定了base_url，teststep中的url只能设置相对路径部分。如果要在不同的SUT环境之间切换，这一点尤其有用。</p>
<h3 id="variables">variables（可选）</h3>
<p>指定testcase的公共变量。每个teststep都可以引用未在步骤变量中设置的配置变量。换句话说，步骤变量比配置变量具有更高的优先级。</p>
<h3 id="verify">verify（可选）</h3>
<p>指定是否验证服务器的TLS证书。如果我们想记录testcase执行的HTTP流量，这一点特别有用，因为如果verify未设置或设置为True，则会发生SSLError。</p>
<p>SSLError（SSLCertVerificationError（1，“[SSL:证书验证失败]证书验证失败：证书链中的自签名证书（_SSL.c:1076）”</p>
<h3 id="export">export（可选）</h3>
<p>指定testcase的导出会话变量。将每个测试用例视为一个黑箱，配置变量是输入部分，配置输出是输出部分。特别是，当一个测试用例在另一个测试用例的步骤中被引用，并且将被提取一些会话变量以在后续的测试步骤中使用时，那么提取的会话变量应该在配置导出部分中配置。</p>
<h2 id="teststeps">二、teststeps</h2>
<p>每个测试用例都应该有一个或多个有序的测试步骤（列表[Step]），每个步骤都对应于一个API请求或另一个测试用例引用调用。</p>
<p><img alt="img" src="https://docs.httprunner.org/images/httprunner-testcase.png" /></p>
<blockquote>
<p>注意：为了简化，HttpRunnerV2.x中的API概念已被弃用。可以将API视为只有一个请求步骤的测试用例。</p>
</blockquote>
<h3 id="1-runrequestname">1. RunRequest(name)</h3>
<p>RunRequest用于向API发出请求，并对响应执行一些提取或验证。</p>
<p>RunRequest的参数名称用于指定teststep名称，该名称将显示在执行日志和测试报告中。</p>
<h4 id="with_variables">.with_variables</h4>
<p>指定teststep变量。每个步骤的变量都是独立的，因此如果要在多个步骤中共享变量，则应在配置变量中定义变量。此外，step变量将覆盖配置变量中具有相同名称的变量。</p>
<h4 id="methodurl">.method（url）</h4>
<p>指定HTTP方法和SUT的url。这些参数对应于requests.request的方法和url参数。</p>
<p>若在配置中设置了基本url，则url只能设置相对路径部分。</p>
<h4 id="with_params">.with_params</h4>
<p>为请求url指定查询字符串。这与requests.request的params参数相对应。</p>
<h4 id="with_headers">.with_headers</h4>
<p>为请求指定HTTP头。这与requests.request的headers参数相对应。</p>
<h4 id="with_cookies">.with_cookies</h4>
<p>指定HTTP请求cookie。这与requests.request的cookies参数相对应。</p>
<h4 id="whth_data">.whth_data</h4>
<p>指定HTTP请求正文。这与requests.request的数据参数相对应。</p>
<h4 id="with_json">.with_json</h4>
<p>在json中指定HTTP请求主体。这与requests.request的json参数相对应。</p>
<h3 id="2-extract">2. extract</h3>
<h4 id="with_jmespath">.WITH_JMESPATH</h4>
<p>使用jmespath提取JSON响应消息体。</p>
<blockquote>
<p>with_jmespath(jmes_path, var_name)</p>
</blockquote>
<ul>
<li>
<p>jmes_path：jmespath表达式，有关详细信息，请参阅jmespath教程</p>
</li>
<li>
<p>var_name：存储提取值的变量名，可供后续测试步骤引用</p>
</li>
</ul>
<h3 id="3-validate">3. validate</h3>
<h4 id="assert_xxx">.ASSERT_XXX</h4>
<p>使用jmespath提取JSON响应主体，并使用预期值进行验证。</p>
<blockquote>
<p>assert_XXX(jmes_path, expected_value, message)</p>
</blockquote>
<ul>
<li>
<p>jmes_path：jmespath表达式，有关详细信息，请参阅jmespath教程</p>
</li>
<li>
<p>expected_value：此处也可以使用指定的期望值、变量或函数引用</p>
</li>
<li>
<p>message（可选）：用于指示断言错误原因</p>
</li>
</ul>
<p>下图显示了HttpRunner内置验证程序：</p>
<p><img alt="img" src="https://docs.httprunner.org/images/httprunner-step-request-validate.png" /></p>
<h3 id="4-runtestcasename">4. RunTestCase（name）</h3>
<p>RunTestCase在一个步骤中用于引用另一个testcase调用。</p>
<p>RunTestCase的参数名称用于指定teststep名称，该名称将显示在执行日志和测试报告中。</p>
<h4 id="with_variables_1">.with_variables</h4>
<p>与RunRequest的相同。使用_变量。</p>
<h4 id="call">.call</h4>
<p>指定引用的testcase类。</p>
<h4 id="export_1">.export</h4>
<p>指定要从引用的testcase导出的会话变量名。导出的变量可由后续测试步骤引用。</p>
<pre><code class="language-python">import os
import sys

sys.path.insert(0, os.getcwd())

from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase

from examples.postman_echo.request_methods.request_with_functions_test import (
    TestCaseRequestWithFunctions as RequestWithFunctions,
)


class TestCaseRequestWithTestcaseReference(HttpRunner):
    config = (
        Config(&quot;request methods testcase: reference testcase&quot;)
        .variables(
            **{
                &quot;foo1&quot;: &quot;testsuite_config_bar1&quot;,
                &quot;expect_foo1&quot;: &quot;testsuite_config_bar1&quot;,
                &quot;expect_foo2&quot;: &quot;config_bar2&quot;,
            }
        )
        .base_url(&quot;https://postman-echo.com&quot;)
        .verify(False)
    )

    teststeps = [
        Step(
            RunTestCase(&quot;request with functions&quot;)
            .with_variables(
                **{&quot;foo1&quot;: &quot;testcase_ref_bar1&quot;, &quot;expect_foo1&quot;: &quot;testcase_ref_bar1&quot;}
            )
            .call(RequestWithFunctions)
            .export(*[&quot;foo3&quot;])
        ),
        Step(
            RunRequest(&quot;post form data&quot;)
            .with_variables(**{&quot;foo1&quot;: &quot;bar1&quot;})
            .post(&quot;/post&quot;)
            .with_headers(
                **{
                    &quot;User-Agent&quot;: &quot;HttpRunner/${get_httprunner_version()}&quot;,
                    &quot;Content-Type&quot;: &quot;application/x-www-form-urlencoded&quot;,
                }
            )
            .with_data(&quot;foo1=$foo1&amp;foo2=$foo3&quot;)
            .validate()
            .assert_equal(&quot;status_code&quot;, 200)
            .assert_equal(&quot;body.form.foo1&quot;, &quot;bar1&quot;)
            .assert_equal(&quot;body.form.foo2&quot;, &quot;bar21&quot;)
        ),
    ]


if __name__ == &quot;__main__&quot;:
    TestCaseRequestWithTestcaseReference().test_start()
</code></pre>
                
              
              
                


              
            </article>
          </div>
        </div>
        
      </main>
      
        
<footer class="md-footer">
  
    <nav class="md-footer__inner md-grid" aria-label="Footer">
      
        
        <a href="../33-%E5%BD%95%E5%88%B6%E5%B9%B6%E7%94%9F%E6%88%90%E6%B5%8B%E8%AF%95%E6%8A%A5%E5%91%8A/" class="md-footer__link md-footer__link--prev" aria-label="上一页: 录制并生成测试用例" rel="prev">
          <div class="md-footer__button md-icon">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12z"/></svg>
          </div>
          <div class="md-footer__title">
            <div class="md-ellipsis">
              <span class="md-footer__direction">
                上一页
              </span>
              录制并生成测试用例
            </div>
          </div>
        </a>
      
      
        
        <a href="../35-%E8%BF%90%E8%A1%8C%E6%B5%8B%E8%AF%95%E7%94%A8%E4%BE%8B/" class="md-footer__link md-footer__link--next" aria-label="下一页: 运行测试用例" rel="next">
          <div class="md-footer__title">
            <div class="md-ellipsis">
              <span class="md-footer__direction">
                下一页
              </span>
              运行测试用例
            </div>
          </div>
          <div class="md-footer__button md-icon">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4z"/></svg>
          </div>
        </a>
      
    </nav>
  
  <div class="md-footer-meta md-typeset">
    <div class="md-footer-meta__inner md-grid">
      <div class="md-footer-copyright">
        
        
          Made with
          <a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener">
            Material for MkDocs
          </a>
        
        
      </div>
      
    </div>
  </div>
</footer>
      
    </div>
    <div class="md-dialog" data-md-component="dialog">
      <div class="md-dialog__inner md-typeset"></div>
    </div>
    <script id="__config" type="application/json">{"base": "../..", "features": [], "translations": {"clipboard.copy": "\u590d\u5236", "clipboard.copied": "\u5df2\u590d\u5236", "search.config.lang": "ja", "search.config.pipeline": "trimmer, stemmer", "search.config.separator": "[\\uff0c\\u3002]+", "search.placeholder": "\u641c\u7d22", "search.result.placeholder": "\u952e\u5165\u4ee5\u5f00\u59cb\u641c\u7d22", "search.result.none": "\u6ca1\u6709\u627e\u5230\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.one": "\u627e\u5230 1 \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.other": "# \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.term.missing": "Missing", "select.version.title": "Select version"}, "search": "../../assets/javascripts/workers/search.fcfe8b6d.min.js", "version": null}</script>
    
    
      <script src="../../assets/javascripts/bundle.b1047164.min.js"></script>
      
    
  </body>
</html>